package com.wondertek.poms.sync.manager.main;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.SyncTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * <p><b>SubSetManager Description:</b> 子集流程</p>
 * @author douzi
 * <b>DATE</b> 2019年9月6日 下午4:06:53
 */
@Slf4j
@Component
public class SpecialSubSetManager extends ParentManager {
    @Autowired
    NoProductMainAssertManager mainAssertManager;
    @Autowired
    IContentService contentService;

    /**
     * <p><b>Title:</b> liveBillProcess</p>
     * <p><b>Description:</b> 子集流程</p>
     *
     * @param vo 内容对象
     * @author douzi
     */
    public void subSetProcess(Content c, SyncTask st, List<List<PrdInfo>> special) {
        switch (c.getStatus().intValue()) {
            case Constants.ASSET_STATUS_0:
            case Constants.ASSET_STATUS_1:
            case Constants.ASSET_STATUS_3:
                subSetUpdateProcess(c, st, special);
                break;
            case Constants.ASSET_STATUS_2:
            	mainDelProcess(st, "special-subset");
                break;
            default:
            	log.info("父子包子集主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() +  "|不属于任何一种媒资状态:" + c.getStatus());
                break;
        }
    }

	/**
     * <p><b>Title:</b> subSetProcess</p>
     * <p><b>Description:</b> 子集媒资流程 </p>
     *
     * @param vo 内容对象
     * @author douzi
     */
    private void subSetUpdateProcess(Content c, SyncTask st, List<List<PrdInfo>> special) {
    	for(List<PrdInfo> piList : special) {
			if(piList != null && piList.size()>0) {
				for (int i = piList.size() - 1; i >= 0; i--) {// V15,V5,V0依次匹配，匹配上一个，其他不匹配
					PrdInfo pi = piList.get(i);
					List<PrdCont> dbPPcs = prdContService.findPcByContentIdInPrdInfo(c.getSerialContentId(), pi.getPrdInfoId());
					
					if (dbPPcs == null || dbPPcs.size() <= 0) {
		    			log.info("父子包子集主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目不存在:" + c.getLiveContentId() + "|商品包：" + pi.getPrdInfoId());
						continue;
					} else if (dbPPcs.size() == 1) {
			    		pcProcess(c, pi, dbPPcs, piList);
		    			// 下线其它包子集节目
						delOtherPrdInfo(c, pi, piList);
			    		return;
			    	} else {
			    		//TODO 去重壳后在入包 暂时不做
		        		log.error("父子包子集主流程|媒资:" + st.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目存在多个:" + c.getLiveContentId() + "|个数：" + dbPPcs.size());
		        		return;
			    	}
					
				}
			}
    	}
    }
    
    /**
	 * <p><b>Title:</b> pcProcess</p>
	 * <p><b>Description:</b> 子集根据数据库是否存在走后续流程</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @param dbPPcs
	 * @param prdInfos
	 */
	private void pcProcess(Content c, PrdInfo prdInfo, List<PrdCont> dbPPcs, List<PrdInfo> prdInfos) {
		// 先获取父子包中子集对象
		List<PrdCont> prdContDbs = new ArrayList<>();
		for (int i = prdInfos.size() - 1; i >= 0; i --) {
			PrdInfo pi = prdInfos.get(i);
			List<PrdCont> pcDbs = prdContService.getPrdContByMmsIdInPrdInfo(c.getAssetId(), pi.getPrdInfoId());
			prdContDbs.addAll(pcDbs);
		}
		log.info("子集父子包流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() +  "|父子包中数据库中媒资数量:" + prdContDbs.size() + "|版权id:" + c.getCopyrightObjectId());
		
		if (prdContDbs.size() <= 0) {
            inPrdInfo(prdInfo, c, null);
        } else if (prdContDbs.size() == 1) {
        	PrdCont pcDb = prdContDbs.get(0);
        	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
        	if (pcDb.getContentId().equals(c.getContentId())) {
                inPrdInfo(prdInfo, c, pcDb);
        	} else {
        		findOptimalPrdCont(c, prdInfo, pcDb, "父子包-子集");
        	}
        } else {
			//TODO 去重子集后在入包 暂时不做
			log.error("子集父子包流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|壳对应节目存在多个:" + c.getLiveContentId() + "|个数：" + dbPPcs.size());
		}
	}
}
